//
//  ByCoreCryptDes.m
//  beibei_ios
//
//  Created by hebidu on 16/4/22.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ByCoreCryptDes.h"

@implementation ByCoreCryptDes


+(NSString *)desEncode:(NSString *)text :(NSString *)key {
    
    NSString *ciphertext = nil;
    NSData * textData = [text dataUsingEncoding:NSUTF8StringEncoding];
    
    const void *textBytes = textData.bytes;
    NSUInteger dataLength = textData.length;
    NSUInteger bufferLen = floor(dataLength / kCCKeySizeDES);
    
    bufferLen = 8* (bufferLen+1);
    //    NSLog(@"dataLength=%zu",dataLength);
    //    NSLog(@"bufferLen=%zu",bufferLen);
    //    NSLog(@"textData length =%zu",textData.length);
    unsigned char buffer[bufferLen];
    
    memset(buffer, 0, sizeof(char));
    Byte iv[] = {1,2,3,4,5,6,7,8};
    
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(
                                          kCCEncrypt,
                                          kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding|kCCOptionECBMode,
                                          [key UTF8String],
                                          kCCKeySizeDES,
                                          iv,
                                          textBytes, textData.length,
                                          buffer, bufferLen,
                                          &numBytesEncrypted);
    
//    NSLog(@"numBytesEncrypted =%zu",numBytesEncrypted);
    
    if (cryptStatus == kCCSuccess) {
        
        NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
        
        ciphertext = [[NSString alloc] initWithData:[ByGTMBase64 encodeData:data] encoding:NSUTF8StringEncoding];
        
    }
    else if(cryptStatus == kCCBufferTooSmall){
        bufferLen = numBytesEncrypted;
        
        NSLog(@"kCCBufferTooSmall******");
        //        NSLog(@"kCCBufferTooSmall******");
        //        NSLog(@"kCCBufferTooSmall******");
        //        NSLog(@"kCCBufferTooSmall******");
        //        [self desEncode:text :key :bufferLen];
        
        return nil;
    }
    
    return ciphertext;
    
}


+ (NSString *)desDecrypt:(NSString *)text WithKey:(NSString *)key
{
    
    NSData* cipherData = [ByGTMBase64 decodeString:text :NSUTF8StringEncoding];
    
    const char *buffer[cipherData.length];
    memset(buffer, 0, sizeof(char));
    
    size_t numBytesDecrypted = 0;
    
    Byte iv[] = {1,2,3,4,5,6,7,8};
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                          kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          [key UTF8String],
                                          kCCKeySizeDES,
                                          iv,
                                          [cipherData bytes],[cipherData length],
                                          buffer, cipherData.length,
                                          &numBytesDecrypted);
    
    //    NSLog(@"cipherData.length=%zu",cipherData.length);
    //    NSLog(@"numBytesDecrypted=%zu",numBytesDecrypted);
    //    NSLog(@"cryptStatus=%d",cryptStatus);
    
    NSString* plainText = nil;
    if (cryptStatus == kCCSuccess) {
        NSData * data = [NSData dataWithBytes:buffer length:numBytesDecrypted];
        plainText = [[NSString alloc ] initWithData:data encoding:NSUTF8StringEncoding];
        
    }
    else if(cryptStatus == kCCBufferTooSmall){
        return nil;
    }
    
    return plainText;
    
}



@end
